package com.ruoyi.project.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.utils.ServletUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import javax.annotation.Resource;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.project.domain.FinSubject;
import com.ruoyi.project.service.IFinSubjectService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 科目Controller
 * 
 * @author abel
 * @date 2024-10-15
 */
@RestController
@RequestMapping("/project/FinSubject")
public class FinSubjectController extends BaseController
{
    @Resource
    private IFinSubjectService finSubjectService;

    /**
     * 查询科目列表
     */
    @PreAuthorize("@ss.hasPermi('project:FinSubject:list')")
    @GetMapping("/list")
    public TableDataInfo list(FinSubject finSubject)
    {
        startPage();
        finSubject.setPackageId(ServletUtils.getPackageId());
        List<FinSubject> list = finSubjectService.selectFinSubjectList(finSubject);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('project:FinSubject:list')")
    @GetMapping("/listFinSubjectTreeSelect")
    public AjaxResult listFinSubjectTreeSelect() {
        Long packageId = ServletUtils.getPackageId();
        List<TreeSelect> treeSelectList = finSubjectService.listTreeSelectByPackageId(packageId);
        return AjaxResult.success(treeSelectList);
    }

    /**
     * 导出科目列表
     */
    @PreAuthorize("@ss.hasPermi('project:FinSubject:export')")
    @Log(title = "科目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FinSubject finSubject)
    {
        List<FinSubject> list = finSubjectService.selectFinSubjectList(finSubject);
        ExcelUtil<FinSubject> util = new ExcelUtil<FinSubject>(FinSubject.class);
        util.exportExcel(response, list, "科目数据");
    }

    /**
     * 获取科目详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:FinSubject:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(finSubjectService.selectFinSubjectById(id));
    }

    /**
     * 新增科目
     */
    @PreAuthorize("@ss.hasPermi('project:FinSubject:add')")
    @Log(title = "科目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FinSubject finSubject)
    {
        finSubject.setPackageId(ServletUtils.getPackageId());
        return toAjax(finSubjectService.insertFinSubject(finSubject));
    }

    @PreAuthorize("@ss.hasPermi('project:FinSubject:add')")
    @Log(title = "同步科目月份数据", businessType = BusinessType.INSERT)
    @PostMapping("/syncSubjectMonth")
    public void syncSubjectMonth()
    {
        finSubjectService.syncSubjectMonth(ServletUtils.getPackageId());
    }


    @PreAuthorize("@ss.hasPermi('project:FinSubject:add')")
    @Log(title = "结转损益", businessType = BusinessType.INSERT)
    @PostMapping("/moveSY/{toSubjectId}")
    public void moveSY(@PathVariable Long toSubjectId)
    {
        finSubjectService.moveSY(ServletUtils.getPackageId(),toSubjectId);
    }

    /**
     * 修改科目
     */
    @PreAuthorize("@ss.hasPermi('project:FinSubject:edit')")
    @Log(title = "科目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FinSubject finSubject)
    {
        return toAjax(finSubjectService.updateFinSubject(finSubject));
    }

    /**
     * 删除科目
     */
    @PreAuthorize("@ss.hasPermi('project:FinSubject:remove')")
    @Log(title = "科目", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(finSubjectService.deleteFinSubjectByIds(ids));
    }
}
